package com.whf.android.jar.web.activity;

import android.content.Intent;
import android.net.Uri;
import android.webkit.JavascriptInterface;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.JsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.net.gson.HttpGsonUtils;
import com.whf.android.jar.util.autonavi.AutoNavi;
import com.whf.android.jar.util.autonavi.AutoNaviUtils;
import com.whf.android.jar.util.log.LatteLogger;
import com.whf.android.jar.web.Javascript.LatteJavascript;
import com.whf.android.jar.web.event.Event;
import com.whf.android.jar.web.event.EventManager;

import org.jetbrains.annotations.NotNull;

/**
 * 原生与网页的交互
 *
 * @author hf
 * @version 1.0.1
 */
@SuppressWarnings("unused")
final class LatteActivityInterface implements LatteJavascript {

    private LatteActivityInterface() {
    }

    @NotNull
    static LatteActivityInterface create() {
        //简单工厂模式创建
        return new LatteActivityInterface();
    }

    /**
     * @param params :调用Event管理
     */
    @JavascriptInterface
    public void event(String params) {
        LatteLogger.i("WEB_EVENT：" + params);
        final String action = JsonUtils.getString(params, "action");
        final Event event = EventManager.getInstance().createEvent(action);
        if (event != null) {
            event.setAction(action);
            event.execute(params);
        }
    }

    /**
     * 扫一扫
     */
    @JavascriptInterface
    public void clickOnScan() {
        final String jsonScan = "{\"action\":\"clickOnScan\"}";
        event(jsonScan);
    }

    /**
     * 选择图片
     */
    @JavascriptInterface
    public void clickOnImage() {
        final String jsonScan = "{\"action\":\"clickOnImage\"}";
        event(jsonScan);
    }

    /**
     * 选择文件
     */
    @JavascriptInterface
    public void clickOnFile() {
        final String jsonScan = "{\"action\":\"clickOnFile\"}";
        event(jsonScan);
    }

    /**
     * 定位
     */
    @JavascriptInterface
    public void clickOnLocation() {
        final String jsonLocation = "{\"action\":\"clickOnLocation\"}";
        event(jsonLocation);
    }

    /**
     * @param url：跳转到发浏览器打开url网址
     */
    @JavascriptInterface
    public void clickOnAction(String url) {
        if (url != null) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            ContextCompat.startActivity(Latte.getActivity(), intent, null);
        }
    }

    /**
     * @param dh：导航
     */
    @JavascriptInterface
    public void clickOnDaoHang(String dh) {
        AutoNavi nav = HttpGsonUtils.fromJson(dh, AutoNavi.class);
        if (nav == null) {
            ToastUtils.showLong("传参不符合规范");
            return;
        }
        AutoNaviUtils.navigation(Latte.getActivity(), nav);
    }

    /**
     * @param phone：电话号码（跳转到拨号界面，用户手动点击拨打）
     */
    @JavascriptInterface
    public void callPhone(String phone) {
        final Intent intent = new Intent(Intent.ACTION_DIAL);
        final Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        ContextCompat.startActivity(Latte.getActivity(), intent, null);
    }

    /**
     * @param uri：跳转到发短信界面
     */
    @JavascriptInterface
    public void callShortLetter(String uri) {
        //打电话
        final Intent intent = new Intent(Intent.ACTION_SENDTO);
        final Uri data = Uri.parse(uri);
        intent.setData(data);
        ContextCompat.startActivity(Latte.getActivity(), intent, null);
    }

    /**
     * @param phone：电话号码（跳转到发短信界面）
     * @param body：短信内容
     */
    @JavascriptInterface
    public void callShortLetter(String phone, String body) {
        //发短信
        final String sms = "sms";
        final Uri data = Uri.parse(sms + "to:" + phone);
        final Intent intent = new Intent(Intent.ACTION_SENDTO, data);
        intent.putExtra(sms + "_body", body);
        ContextCompat.startActivity(Latte.getActivity(), intent, null);
    }

    /**
     * 发邮件
     *
     * @param email:接收人
     * @param emailCC:抄送人
     * @param subject:主题
     * @param text:正文
     */
    @JavascriptInterface
    public void callMail(String[] email, String[] emailCC, String subject, String text) {
        //发邮件
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822"); // 设置邮件格式
        intent.putExtra(Intent.EXTRA_EMAIL, email); // 接收人
        intent.putExtra(Intent.EXTRA_CC, emailCC); // 抄送人
        intent.putExtra(Intent.EXTRA_SUBJECT, subject); // 主题
        intent.putExtra(Intent.EXTRA_TEXT, text); // 正文
        Intent chooser = Intent.createChooser(intent, "请选择邮件类应用");
        ContextCompat.startActivity(Latte.getActivity(), chooser, null);
    }

}
